<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>层级配置表</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div class="material-classify">
      <search-btn-view btn-text="查询" @before-open="resetSearch" @search="searchData">
        <template slot="append">
          <el-button size="medium" type="primary" @click="addClassify">新增配置</el-button>
        </template>
        <div slot="search-content">
          <search-form :search="search"></search-form>
        </div>
      </search-btn-view>

      <div class="material-classify-table-div">
        <el-table fit stripe border :data="classifyList" style="width:100%" height="parent">
          <template v-for="field in classifyLevel">
            <el-table-column :label="i18n.getMsg('bomBusiness.materialClassify.classifyLevel.'+field)" :prop="field" :key="'classify_'+field"
              min-width="120px" align="center">
            </el-table-column>
          </template>

          <el-table-column :resizable="false" label="状态" column-key="status" key="status" width="80px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('bomBusiness.materialClassify.status.'+scope.row.status)}}</span>
            </template>
          </el-table-column>

          <el-table-column :resizable="false" label="操作" column-key="handle" key="handle" width="80px" align="center" fixed="right">
            <template slot-scope="scope">
              <template v-if="scope.row.status===1">
                <el-button size="mini" type="danger" @click="updateStatus(scope.row,0)">禁用</el-button>
              </template>
              <template v-else-if="scope.row.status===0">
                <el-button size="mini" type="primary" @click="updateStatus(scope.row,1)">启用</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div style="margin-top:5px;text-align:right" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>
    </div>

    <el-dialog :title="formType==='add'?'新增配置':'修改配置'" :visible.sync="openForm" width="400px" top="10vh" @closed="formClosed">
      <div style="height:calc(80vh - 54px);overflow:auto">
        <material-classify-form v-if="openForm" :type="formType" :classify="selectClassify" @save-success="saveSuccess">
        </material-classify-form>
      </div>
    </el-dialog>
  </div>
</body>

<script src="../../myJs/importFile.js"></script>
<script>
  importFile([
    "/vueSystem/components/css/searchBtnView.css",
    "./css/materialClassifyForm.css"
  ], [
    "/vueSystem/i18n/index.js",
    "/vueSystem/components/searchBtnView.js",
    "/vueSystem/models/baseModel.js"
  ]);

</script>

<script src="../models/materialClassifyModel.js"></script>
<script src="./materialClassifyForm.js"></script>
<script src="./searchForm.js"></script>

<script>
  var app = new Vue({
    el: '#app',
    data: function () {
      var search = {
        status: ''
      };
      return {
        loading: false,
        classifyList: [],
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        },
        classifyLevel: MaterialClassifyModel.classifyLevel,
        openForm: false,
        formType: '',
        selectClassify: null
      };
    },

    created: function () {
      this.setClassifyList(true);
    },

    methods: {
      setClassifyList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize
        };

        var search = this.search;
        var status = search.status;

        if (status !== '') {
          postData.status = status;
        }

        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/getLevelRankList', postData).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var classifyList = res.returnData;
          self.classifyList = classifyList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setClassifyList(true);
      },

      pageChange: function () {
        this.setClassifyList();
      },

      addClassify: function () {
        this.formType = 'add';
        this.openForm = true;
      },

      updateClassify: function (classify) {
        this.formType = 'update';
        this.selectClassify = classify;
        this.openForm = true;
      },

      formClosed: function () {
        this.formType = '';
        this.selectClassify = null;
      },

      saveSuccess: function () {
        this.openForm = false;
        this.setClassifyList();
      },

      updateStatus: function (classify, status) {
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/updateLevelRank', {
          id: classify.id,
          status: status
        }).then(function () {
          self.loading = false;
          var msg = status === 0 ? '该配置成功禁用' : '该配置成功启用';
          self.$alert(msg, '信息', {
            callback: function () {
              self.setClassifyList();
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      MaterialClassifyForm: MaterialClassifyForm,
      SearchForm: SearchForm
    }
  });

</script>

<style>
  .material-classify {
    padding: 10px 20px 5px;
    height: calc(100% - 15px);
    display: flex;
    flex-direction: column;
  }

  .material-classify .material-classify-table-div {
    flex: 1;
    overflow: auto;
  }

</style>

</html>
